home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / MacQForth 1.0 / documentation / Lessons / LESSON 2 < prev    next >
Encoding:
Text File  |  1995-03-24  |  11.3 KB  |  140 lines  |  [TEXT/ALFA]

  1. if 
  2.     it is true (-1) then the code between the 'if' and the 'else' or 
  3.     'then' (if no 'else') is executed.  If false, the code between the 
  4.     'else' and 'then' (or nothing if no 'else') is executed.  Ifs can be 
  5.     nested to provide a selection:
  6.     
  7.     e.g.
  8.     
  9.        : typeKEY
  10.             key   ( 'key' waits for the user to type a key and puts the )
  11.                   ( ASCII code on the stack. )
  12.             dup 13 = if  drop ." <CR>"  cr  else
  13.             dup 27 = if  drop ." <ESC>"     else
  14.                 emit  then then  ( 'emit' prints the character whose ASCII )
  15.                                  ( code is on the stack ) ;
  16.         : manyKEYs  40 0 do typeKEY  loop ;
  17.  
  18.      Notice that the above is really a compact form of:
  19.      
  20.         : typeKEY
  21.             key
  22.             
  23.             dup 13 = if
  24.               drop ." <CR>" cr
  25.             else  dup 27 = if
  26.                     drop ." <ESC>"
  27.                   else
  28.                     emit
  29.                   then
  30.             then ;
  31.  
  32.      Note the DROP after the IF in the first two cases.  This is 
  33.      necessary to remove the extra character (either ASCII 13 or ASCII 
  34.      27).  It is not needed in the last case since EMIT consumes it.
  35.  
  36.  
  37. Using the return stack
  38. ----------------------
  39.  
  40.    The return stack is a second Forth stack used to hold the address of 
  41.    the word to return to when the current word is done executing.  
  42.    While it is generally not a wise idea to fiddle with the return stack 
  43.    it can be used to hold a temporary value or two provided the value is 
  44.    removed before the word ends.  The words >R, R@ (or R), and R> put the 
  45.    top data stack item on the return stack, copy it from the return 
  46.    stack, and pull it off the return stack respectively.
  47.    
  48.    e.g.
  49.    
  50.       : dist+  ( a b c -- a*c+b*c )
  51.           dup >r    ( save c on return stack )
  52.           *         ( multiply b and c )
  53.           swap r> * ( multiply a and c )
  54.           +  ;      ( add them together )
  55.           
  56.    Be sure to remove anything put on the return stack before the word 
  57.    exits!  Failure to do so will undoubtedly cause a crash.  Forth 
  58.    teaches you not to fear crashing the computer :)
  59.  
  60.    N.B.  QForth's r@ word does not work properly.  In its place use R@ as 
  61.    defined in the file EXTEND.4TH.
  62.  
  63.  
  64.  
  65. Word summary
  66. ------------
  67.  
  68.    A summary of the new Forth words introduced in this lesson.
  69.    
  70.    :            ( : name -- )  start a new word definition.
  71.    ;            ( -- ) end a colon definition.
  72.    ."           ( : " -- ) compile a string to be displayed on output.
  73.    CR           ( -- )  output a carraige return.
  74.    FORGET       ( : word -- )  remove word and all words after it from dict.
  75.    '            ( : word -- addr ) place address of word on stack.
  76.    EXECUTE      ( addr -- ) execute word (or code) at addr.  
  77.    DO           ( hi lo -- )  start a DO loop.  Definition only.
  78.    LOOP         ( -- )  end a DO loop. Definition only.
  79.    +LOOP        ( n -- )  end a loop adding n to the index.  Definition only.
  80.    BEGIN        ( -- )  mark the start of a while or until loop.
  81.    WHILE        ( b -- )  while b is true to code between while and repeat.
  82.    REPEAT       ( -- ) mark the end of a WHILE loop.
  83.    UNTIL        ( b -- ) end an UNTIL loop when b is true.
  84.    AGAIN        ( -- )  mark the end of an infinite loop.
  85.    IF           ( b -- )  do true clause if b it true.
  86.    ELSE         ( -- )  mark between true and false parts of an if.
  87.    THEN         ( -- )  mark the end of an if.
  88.    I, J         ( -- n ) return current DO loop index. J is for an inner loop.
  89.    PAGE         ( -- ) clear the screen.
  90.    <, >, =, <>  ( m n -- b ) true if m op n is true, 0 otherwise.
  91.    AND          ( m n -- b ) true if m and n are true, 0 otherwise.
  92.    OR           ( m n -- b ) true if either m or n or both are true.
  93.    NOT          ( m -- not m )  if m true then false else true.
  94.    0=, 0<, 0>   ( m -- b )  compare m against zero, leaves true or false.
  95.    1-           ( n -- n-1 )  subtract one from top of stack.
  96.    KEY          ( -- c )  wait for a keypress and put ASCII code on stack.
  97.    >R           ( n -- )  push n on the return stack.
  98.    R@           ( -- n )  copy n from the return stack.
  99.    R>           ( -- n )  pull n from the return stack.
  100.    
  101.  
  102.  
  103. Exercises
  104. ---------
  105.  
  106.  
  107.    Using the words introduced so far, including those from lesson 1, 
  108.    write valid Forth words that do the following:
  109.    
  110. 2.1  Reverse BLAST, counting up from zero to ten.  Use a DO .. LOOP.
  111. 2.2  As above, but use a BEGIN .. WHILE .. REPEAT loop.
  112. 2.3  As above, but use a BEGIN .. UNTIL loop.
  113.  
  114. 2.4  Rewrite typeKEY so that it will echo alphanumeric characters (ASCII 
  115.      code > 31) and will indicate a return by <CR>, an escape by <ESC>, 
  116.      and all other control characters with ASCII < 27 as <CTRL-letter>.
  117.      So, typing control-G will produce <CTRL-G>.  Use the word manyKEYS 
  118.      to test typeKEY.  Codes between ASCII 26 and ASCII 32 should be 
  119.      ignored.  Use EMIT to output a single character.  Note, uppercase
  120.      letters start at ASCII 65 for 'A'.
  121.      
  122. 2.5  Write the word +CONST that will add a constant value on top of the 
  123.      stack to next three stack items.  +CONST has the following stack 
  124.      effect comment:
  125.      
  126.         +CONST ( i0 i1 i2 k -- i0+k i1+k i2+k )
  127.  
  128.  
  129.  
  130.  
  131.  
  132. \
  133. \ LESSON 2 -- Defining words
  134. \
  135. \ MacQForth lessons
  136. \
  137. \ by Ronald T. Kneusel, see address in READ ME FIRST file.
  138. \
  139. \ March 1995
  140.